package com.mawujun.util;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;


public class SessionFilter  implements Filter {

    public FilterConfig config;

    public void setFilterConfig(FilterConfig config) {
          this.config = config;
    }

    public FilterConfig getFilterConfig() {
          return config;
    }

    public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {

          HttpServletRequest httpreq = (HttpServletRequest) request;
          HttpServletResponse httpres = (HttpServletResponse) response;

          HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper(
                      (HttpServletResponse) response);

          String redirectPath = "login.jsp";
//          String disabletestfilter = config.getInitParameter("disabletestfilter");
//
//          if (disabletestfilter.toUpperCase().equals("Y")) {
//                chain.doFilter(request, response);
//                return;
//          }
          //System.out.println(httpreq.getContextPath()+"/"+httpreq.getRequestURL());
          
          if(httpreq.getRequestURL().indexOf("/logon.do")!=-1 || httpreq.getRequestURL().indexOf("/login.jsp")!=-1 || httpreq.getRequestURL().indexOf("/ext/")!=-1
        		  ||httpreq.getRequestURL().indexOf("/findZhangt.do")!=-1||httpreq.getRequestURL().indexOf("/logonReady.jsp")!=-1) {
        	  chain.doFilter(request, response);
        	  return;
          }
          

          User user = (User)httpreq.getSession().getAttribute("user");
          if (user == null) {
        	  wrapper.sendRedirect(redirectPath);
          } else {
        	  
              chain.doFilter(request, response);
          }
    }

    public void destroy() {
          this.config = null;
    }

    public void init(FilterConfig filterConfig) throws ServletException {
          this.config = filterConfig;
    }


}
